﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Site.Models
{
    public class StatisticRepository : IStatisticRepository
    {
        private BMSiteDBEntities _db = new BMSiteDBEntities();

        public IEnumerable<Statistic> GetUsersStatistic() { return null; }
        public IEnumerable<Statistic> GetClansStatistic() { return null; }
        public IEnumerable<GameType> GetGameTypes()
        {
            return (from r in _db.GameType
                    select r).ToList();
        }
        public IEnumerable<SelectListItem> GetGameTypesDDL(string id)
        {
            List<SelectListItem> list = new List<SelectListItem>();

            return
                _db.GameType.OrderBy(r => r.TypeName)
              .Select(r =>
                  new SelectListItem
                  {
                      Selected = (r.TypeName == id),
                      Text = r.TypeName,
                      Value = r.TypeName.ToString()
                  });

        }


        public IEnumerable<SelectListItem> GetStatTypeDDL()
        {
            List<SelectListItem> list = new List<SelectListItem>();

            list.Add(new SelectListItem { Text = "Utilisateur", Value = "1" });

            list.Add(new SelectListItem { Text = "Clan", Value = "2" });

            list.Add(new SelectListItem { Text = "Global", Value = "3", Selected = true });
            return list;

        }

        public List<SelectListItem> GetGameTypesDDL()
        {

            var items = from r in _db.GameType
                        select r;
            List<SelectListItem> itemsSelect = new List<SelectListItem>();
            foreach (var item in items)
            {
                itemsSelect.Add(new SelectListItem
                           {
                               Value = item.TypeId.ToString(),
                               Text = item.TypeName
                           });
            }

            return itemsSelect;


        }



        public IEnumerable<CommonStats> GetClansStats(int gt)
        {
            return (from s in _db.STATISTIC
                    join c in _db.CLAN
                    on s.STAT_CLAN_ID equals c.CLAN_ID into list
                    where s.STAT_CLAN_ID != null
                    && s.STAT_GAMETYPE == gt
                    select new CommonStats
                    {
                        StatId = (int)s.STAT_ID,
                        ClanName = s.CLAN.CLAN_NAME,
                        ClanId = (Guid)s.CLAN.CLAN_ID,
                        Rank = (int)s.STAT_RANK,
                        WonGames = (int)s.STAT_WONGAMES,
                        LostGames = (int)s.STAT_LOSTGAMES,
                        CanceledGames = (int)s.STAT_ABANDONEDGAMES,
                        GameType = (int)s.STAT_GAMETYPE

                    }).Take(15).ToList();
        }

        public IEnumerable<CommonStats> GetUsersStats(int gt)
        {
            return (from s in _db.STATISTIC
                    join c in _db.aspnet_Users
                    on s.STAT_USER_ID equals c.UserId
                    where s.STAT_USER_ID != null
                    && s.STAT_GAMETYPE == gt
                    select new CommonStats
                    {
                        StatId = (int)s.STAT_ID,
                        ClanName = s.CLAN.CLAN_NAME,
                        UserName = c.UserName,
                        UserId = (Guid)c.UserId,
                        Rank = (int)s.STAT_RANK,
                        WonGames = (int)s.STAT_WONGAMES,
                        LostGames = (int)s.STAT_LOSTGAMES,
                        CanceledGames = (int)s.STAT_ABANDONEDGAMES,
                        GameType = (int)s.STAT_GAMETYPE

                    }).Take(15).ToList();
        }
        public IEnumerable<CommonStats> GetStats(string statType, int gametype)
        {

            return (from s in _db.STATISTIC
                    join c in _db.aspnet_Users
                    on s.STAT_USER_ID equals c.UserId
                    where s.STAT_USER_ID != null
                    && s.STAT_GAMETYPE == gametype
                    select new CommonStats
                    {
                        StatId = (int)s.STAT_ID,
                        ClanName = s.CLAN.CLAN_NAME,
                        UserName = c.UserName,
                        UserId = (Guid)c.UserId,
                        Rank = (int)s.STAT_RANK,
                        WonGames = (int)s.STAT_WONGAMES,
                        LostGames = (int)s.STAT_LOSTGAMES,
                        CanceledGames = (int)s.STAT_ABANDONEDGAMES,
                        GameType = (int)s.STAT_GAMETYPE

                    }).Take(15).ToList();
        }
        public IEnumerable<STATISTIC> GetStatByType(int i, Guid id)
        {
            return _db.STATISTIC.Where(r => r.STAT_GAMETYPE == i && r.STAT_USER_ID == id);

        }
        public StatsDetails GetUserDetailedStats(Guid id)
        {
            StatsDetails sd = new StatsDetails();
            STATISTIC St = GetStatByType(1, id).First();
            sd.won1vall = (int)St.STAT_WONGAMES;
            sd.lost1vall = (int)St.STAT_LOSTGAMES;
            sd.canceled1vall = (int)St.STAT_ABANDONEDGAMES;
            STATISTIC St2 = GetStatByType(2, id).First();
            sd.won1v1 = (int)St2.STAT_WONGAMES;
            sd.lost1v1 = (int)St2.STAT_LOSTGAMES;
            sd.canceled1v1 = (int)St2.STAT_ABANDONEDGAMES;
            STATISTIC St3 = GetStatByType(3, id).First();
            sd.won2v2f = (int)St3.STAT_WONGAMES;
            sd.lost2v2f = (int)St3.STAT_LOSTGAMES;
            sd.canceled2v2f = (int)St3.STAT_ABANDONEDGAMES;
            STATISTIC St4 = GetStatByType(4, id).First();
            sd.won2v2a = (int)St4.STAT_WONGAMES;
            sd.lost2v2a = (int)St4.STAT_LOSTGAMES;
            sd.canceled2v2a = (int)St4.STAT_ABANDONEDGAMES;
            sd.canceledTotal = sd.canceled1v1 + sd.canceled1vall + sd.canceled2v2a + sd.canceled2v2f;
            sd.wonTotal = sd.won1v1 + sd.won1vall + sd.won2v2f + sd.won2v2a;
            sd.lostTotal = sd.lost1v1 + sd.lost1vall + sd.lost2v2f + sd.lost2v2a;
            sd.name = (from u in _db.aspnet_Users where u.UserId == id select u.UserName).First();
            return sd;
        }

        public void ResetStatByType(Guid id, int type)
        {
            var stat1 = (from s in _db.STATISTIC
                         where (s.STAT_CLAN_ID == id
                         || s.STAT_USER_ID == id)
                         && s.STAT_GAMETYPE == type
                         select s).FirstOrDefault();
            stat1.STAT_LOSTGAMES = 0;
            stat1.STAT_WONGAMES = 0;
            stat1.STAT_ABANDONEDGAMES = 0;
            _db.ApplyCurrentValues(stat1.EntityKey.EntitySetName, stat1);
            _db.SaveChanges();

        }
        public void ResetStat(Guid id)
        {
            for (int i = 1; i <= 4; i++)
            {
                ResetStatByType(id, i);
            }
        }
    }
}